package com.dd.ent.car.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.dd.ent.car.R;

public class ProgressAlertDialog extends BaseDialog {

    private final View mDialogView;
    private ArcProgress mProgress;

    public ProgressAlertDialog(Context context) {
        super(context, R.style.base_dialog);
        mDialogView = LayoutInflater.from(context).inflate(R.layout.progress_dlg, null);
        setContentView(mDialogView);
        initUI(context);
    }

    private void initUI(final Context context) {
        mProgress = (ArcProgress) mDialogView.findViewById(R.id.myProgress);
        mProgress.setOnCenterDraw(new ArcProgress.OnCenterDraw() {
            @Override
            public void draw(Canvas canvas, RectF rectF, float x, float y, float storkeWidth, int progress) {
                Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                textPaint.setStrokeWidth(35);
                textPaint.setColor(context.getResources().getColor(R.color.blue_50));
                textPaint.setTextSize(context.getResources().getDimension(R.dimen.font_24));
                String progressStr = String.valueOf(progress + "%");
                float textX = x - (textPaint.measureText(progressStr) / 2);
                float textY = y - ((textPaint.descent() + textPaint.ascent()) / 2);
                canvas.drawText(progressStr, textX, textY, textPaint);
            }
        });
    }

    public void setProgress(int progress) {
        if (mProgress != null) {
            mProgress.setProgress(progress);
        }
    }

    public int getProgress() {
        if (mProgress != null) {
            return mProgress.getProgress();
        }

        return 0;
    }

    /**
     * 判断当前用户触摸是否超出了Dialog的显示区域
     *
     * @param context
     * @param event
     * @return
     */
    protected boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

}
